#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    init_db();

    connect(ui->pushButton_query, &QPushButton::clicked, this, &Widget::showUsers);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::init_db(){
    if(dbhelper::get_instance().init_db()){
        qDebug() << "connect database success...";
        return;
    }else{
        qDebug() << "connect database fail...";
    }
}

void Widget::showUsers(){
    QList<User> users = dbhelper::get_instance().getAllUsers();
    if(0 == users.length()){
        qDebug() << "get user info error";
    }

    ui->textEdit->append(QString("id \t name \t password TotalAmount ConsumeAmount"));
    for(const User &user : users){
       QString strTotalAmount = QString::number(user.getTotalAmount());
       QString strConsumeAmount = QString::number(user.getConsumeAmount());
       ui->textEdit->append(user.getId()+"\t"+user.getName()+"\t"+user.getPassword()+
                            "\t\t"+strTotalAmount+"\t"+strConsumeAmount);
    }

    return;
}

